<?php

function parent_get_current_user_session($key = '')
{
    $request = request();
    if (!$token = $request->header('X-Token')) {
        return [];
    }
    $sessionData = cache('PARENT_TOKEN:'.$token);
    if ($sessionData) {
        if ($key) {
            return isset($sessionData[$key]) ? $sessionData[$key] : [];
        }

        return $sessionData;
    }

    return [];
}

/**
 * 生成token.
 *
 * @return string
 */
function parent_generate_token()
{
    // 请求ip
    $ip = $_SERVER['REMOTE_ADDR'];
    // 时间戳
    $timestamp = $_SERVER['REQUEST_TIME_FLOAT'];
    // salt随机盐
    $salt = uniqid();

    return md5($ip.$timestamp.$salt);
}

/**
 * 获取当前登录的家长ID
 * User: wufeng
 * Date: 2019/5/24.
 *
 * @return int
 */
function parent_get_current_user_id()
{
    return (int) parent_get_current_user_session('user_id');
}

/**
 * 获取当前登录的家长姓名
 * User: wufeng
 * Date: 2019/5/24.
 *
 * @return mixed
 */
function parent_get_current_name()
{
    return parent_get_current_user_session('name');
}

/**
 * 获取当前登录的家长所在校区ID
 * User: wufeng
 * Date: 2019/5/24.
 *
 * @return int
 */
function parent_get_current_campus_id()
{
    return parent_get_current_user_session('campus_id');
}

/**
 * 获取当前登录的家长所在集团ID
 * User: wufeng
 * Date: 2019/5/24.
 *
 * @return int
 */
function parent_get_current_company_id()
{
    return (int) parent_get_current_user_session('company_id');
}

/**
 * 获取当前登录的家长头像
 * User: wufeng
 * Date: 2019/6/4.
 *
 * @return mixed
 */
function parent_get_current_img()
{
    return parent_get_current_user_session('img');
}
